//
//  DWDContentView.swift
//  dwdWb
//
//  Created by Dengdwd on 15/10/11.
//  Copyright © 2015年 dwd. All rights reserved.
//

import UIKit

protocol DWDCoverViewDelegate:NSObjectProtocol {

    func coverViewMakeTitleRotate()

}
class DWDCoverView: UIButton {

    var delegate: DWDCoverViewDelegate?
    
    var iconBack:UIImageView?
    init(customView:UIView){
        super.init(frame: CGRectMake(0, 0, SCREENW, SCREENH))
        addTarget(self, action: "closeCoverView:", forControlEvents: UIControlEvents.TouchUpInside)
        setUpCoverView(customView)
    }

    private func setUpCoverView(customView:UIView) {
        
        backgroundColor = UIColor(white: 1, alpha: 0.1)
        let iconBack = UIImageView(image:UIImage(named: "popover_background"))
        iconBack.size = CGSizeMake(customView.bounds.width + 10, customView.bounds.height + 22)
        iconBack.userInteractionEnabled = true
        self.iconBack = iconBack
        addSubview(iconBack)
        customView.x = 5
        customView.y = 13
        iconBack.addSubview(customView)
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func show(titleBtn:UIButton) {
        self.iconBack?.centerX = self.centerX
        let rect = titleBtn.convertRect(titleBtn.bounds, toView:nil)
        self.iconBack?.y = CGRectGetMaxY(rect)
        
        let window = UIApplication.sharedApplication().windows.last
        window?.addSubview(self)
        
    }
    @objc private func closeCoverView(coverBtn:UIButton) {
    
        if let d = delegate {
        d.coverViewMakeTitleRotate()
        }
        coverBtn.removeFromSuperview()
  
    }
}
